package com.woniu.bh.beautysalon.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.woniu.bh.beautysalon.model.dto.*;
import com.woniu.bh.beautysalon.model.params.AllTechnicianParam;
import com.woniu.bh.beautysalon.model.params.CheckResultParam;
import com.woniu.bh.beautysalon.model.params.ArrangeTechnicianParam;
import com.woniu.bh.beautysalon.model.params.PageParam;
import com.woniu.bh.beautysalon.web.service.ITechnicianService;
import com.woniu.bh.common.core.dto.ResultDto;
import com.woniu.bh.common.core.exception.ProjectException;
import com.woniu.bh.common.core.exception.ProjectExceptionResult;
import com.woniu.bh.common.core.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author Daniel
 * @version 1.0.0
 * @ClassName TechnicianController
 * @createTime 2020年10月13日 14:30:00
 */
@RestController
@RequestMapping("/technician")
@Slf4j
@Api(value = "美容馆接口", tags = "美容馆美容师管理")
public class TechnicianController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ITechnicianService technicianService;

    /**
     * @description: 美容师排行榜
     * @param:
     * @return: com.woniu.project.common.core.dto.ResultDto
     */
    @GetMapping("/technicianRank")
    @ApiOperation("美容师排行")
    public ResultDto technicianRank(HttpServletRequest request) {
        try {
            //解析token获取id
            String token = request.getHeader("X-Token");
            Integer beautysalonId = Integer.parseInt(JwtUtil.parseToken(token).get("beautysalonId").toString());
            List<TechnicianRankDto> list = technicianService.technicianRank(beautysalonId);
            return ResultDto.builder().success(true).code(1).object(list).message("查询成功").build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

    /**
     * @description: 分页条件查看所有注册的美容师
     * @param: allTechnicianParam
     * @return: ResultDto
     */
    @GetMapping("getAllTechnicians")
    @ApiOperation("分页条件查询所有注册的美容师")
    public ResultDto getAllTechnicians(AllTechnicianParam allTechnicianParam) {
        log.info("传入数据{}", allTechnicianParam);
        try {
            IPage<AllTechniciansDto> allTechniciansDto = technicianService.pageQuery(allTechnicianParam.getPageIndex(), allTechnicianParam.getPageSize(), allTechnicianParam.getTechnicianName(), allTechnicianParam.getTechnicianSex(), allTechnicianParam.getBeforeAge(), allTechnicianParam.getAfterAge());
            return ResultDto.builder().success(true).code(1).object(allTechniciansDto).message("查询成功").build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

    /**
     * @description: 美容师详情
     * @param: technicianId
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @GetMapping("/getOneTechnician/{technicianId}")
    @ApiOperation("查看美容师详情")
    public ResultDto getOneTechnician(@PathVariable Integer technicianId) {
        log.info("传入参数:{}", technicianId);
        try {
            TechnicianDetailDto technicianDetailDto = technicianService.getOneTechnician(technicianId);
            return ResultDto.builder().success(true).code(1).object(technicianDetailDto).message("查询成功").build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

    /**
     * @description: 向美容师发起签约请求
     * @param: technicianId
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @PostMapping("/signTechnician/{technicianId}")
    @ApiOperation("像美容师发起签约请求")
    public ResultDto signTechnician(@PathVariable Integer technicianId, HttpServletRequest request) {
        log.info("传入参数:{}", technicianId);

        try {
            //解析token获取id
            String token = request.getHeader("X-Token");
            Integer beautysalonId = Integer.parseInt(JwtUtil.parseToken(token).get("beautysalonId").toString());
            int result = technicianService.signTechnician(beautysalonId, technicianId);
            if (result != -1) {
                return ResultDto.builder().success(true).code(1).object(result).message("申请成功").build();
            }
            throw new ProjectException(ProjectExceptionResult.DATA_NOT_FOUND);
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

    /**
     * @description: 分页条件查询已签约美容师
     * @param: allTechnicianParam
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @GetMapping("getAllSignedTechnicians")
    @ApiOperation("分页条件查询我签约的美容师")
    public ResultDto getAllSignedTechnicians(AllTechnicianParam allTechnicianParam, HttpServletRequest request) {
        log.info("传入数据{}", allTechnicianParam);
        try {
            //解析token获取id
            String token = request.getHeader("X-Token");
            Integer beautysalonId = Integer.parseInt(JwtUtil.parseToken(token).get("beautysalonId").toString());
            IPage<AllTechniciansDto> myTechnicians = technicianService.getAllSignedTechnicians(allTechnicianParam.getPageIndex(), allTechnicianParam.getPageSize(), beautysalonId, allTechnicianParam.getTechnicianName(), allTechnicianParam.getTechnicianSex(), allTechnicianParam.getBeforeAge(), allTechnicianParam.getAfterAge());
            return ResultDto.builder().success(true).code(1).object(myTechnicians).message("查询成功成功").build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

    /**
     * @description: 安排空闲美容师上门
     * @param: arrangeTechnicianParam
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @PostMapping("/assignedTechnicians")
    @ApiOperation("指排美容师")
    public ResultDto assignedTechnicians(ArrangeTechnicianParam arrangeTechnicianParam, HttpServletRequest request) {
        log.info("传入参数:{}", arrangeTechnicianParam);
        try {
            //解析token获取id
            String token = request.getHeader("X-Token");
            Integer beautysalonId = Integer.parseInt(JwtUtil.parseToken(token).get("beautysalonId").toString());
            TechnicianDetailDto oneTechnician = technicianService.getOneTechnician(arrangeTechnicianParam.getTechnicianId());

            System.out.println(oneTechnician.getTechnicianWorkstatus());
            if (oneTechnician.getTechnicianWorkstatus() == 0) {
                technicianService.arrangeTechnician(arrangeTechnicianParam);
                return ResultDto.builder().success(true).code(1).message("成功").build();
            }
            return ResultDto.builder().success(false).code(22001).message("美容师正忙").build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

    /**
     * @description: 解约美容师
     * @param: technicianId
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @PostMapping("/termination/{technicianId}")
    @ApiOperation("解约美容师")
    public ResultDto termination(@PathVariable Integer technicianId) {
        log.info("传入参数:{}", technicianId);
        try {
            int result = technicianService.termination(technicianId);
            if (result > 0) {
                return ResultDto.builder().success(true).code(1).object(result).message("操作成功").build();
            }
            return ResultDto.builder().success(false).code(22001).message("美容师正忙，无法解约").build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

    /**
     * @description: 查询审核表
     * @param: pageParam
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @GetMapping("/getAllCheckTechnicians")
    @ApiOperation("查询审核表")
    public ResultDto getAllCheckTechnicians(PageParam pageParam, HttpServletRequest request) {
        log.info("传入参数:{}", pageParam);
        try {
            //解析token获取id
            String token = request.getHeader("X-Token");
            Integer beautysalonId = Integer.parseInt(JwtUtil.parseToken(token).get("beautysalonId").toString());
            IPage<CheckDto> allCheck = technicianService.getAllCheck(pageParam.getPageIndex(), pageParam.getPageSize(), beautysalonId);
            return ResultDto.builder().success(true).code(1).message("查询成功").object(allCheck).build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

    /**
     * @description: 审核美容师详情查看
     * @param: checkId
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @GetMapping("/getOneCheckTechnician/{checkId}")
    @ApiOperation("审核表美容师详情查看")
    public ResultDto getOneCheck(@PathVariable Integer checkId) {
        log.info("传入参数:{}", checkId);
        try {
            TechnicianCheckDetailDto tcdd = technicianService.getOneCheck(checkId);
            return ResultDto.builder().success(true).code(1).message("查询成功").object(tcdd).build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

    /**
     * @description: 美容师审核通过
     * @param: approvedParam
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @PostMapping("/approved")
    @ApiOperation("通过审核")
    public ResultDto approved(CheckResultParam checkResultParam, HttpServletRequest request) {
        log.info("传入参数:{}", checkResultParam);
        try {
            //解析token获取id
            String token = request.getHeader("X-Token");
            Integer beautysalonId = Integer.parseInt(JwtUtil.parseToken(token).get("beautysalonId").toString());
            int btmId = technicianService.sendOkMsg(checkResultParam.getCheckId(), checkResultParam.getMessage(), beautysalonId);
            if (btmId > 0) {
                return ResultDto.builder().success(true).code(1).message("审核成功").object(btmId).build();
            }
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

    /**
     * @description: 美容师审核拒绝
     * @param:
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @PostMapping("/refuse")
    @ApiOperation("审核不通过")
    public ResultDto refuse(CheckResultParam checkResultParam, HttpServletRequest request) {
        log.info("传入参数:{}", checkResultParam);
        try {
            //解析token获取id
            String token = request.getHeader("X-Token");
            Integer beautysalonId = Integer.parseInt(JwtUtil.parseToken(token).get("beautysalonId").toString());
            int btmId = technicianService.sendNoMsg(checkResultParam.getCheckId(), checkResultParam.getMessage(), beautysalonId);
            if (btmId > 0) {
                return ResultDto.builder().success(true).code(1).message("审核成功").object(btmId).build();
            }
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }

}
